package com.inspur.edp.web.formmetadata.i18n.component.strategy.i18nresourcestrategy;

import com.inspur.edp.lcm.metadata.api.entity.I18nResourceItemCollection;
import com.inspur.edp.web.formmetadata.i18n.component.strategy.namestrategy.ComponentNameFactory;
import com.inspur.edp.web.formmetadata.i18n.component.strategy.namestrategy.ComponentNameType;
import com.inspur.edp.web.formmetadata.i18n.component.strategy.namestrategy.IComponentNameStrategy;
import com.inspur.edp.lcm.metadata.api.entity.I18nResourceItem;
import com.inspur.edp.lcm.metadata.api.entity.I18nResourceItemCollection;
import com.inspur.edp.web.common.utility.StringUtility;
import com.inspur.edp.web.formmetadata.i18n.I18nResourceItemManager;
import com.inspur.edp.web.formmetadata.i18n.component.ComponentUtility;
import com.inspur.edp.web.formmetadata.i18n.component.I18nResourceUtility;
import com.inspur.edp.web.formmetadata.i18n.component.strategy.namestrategy.ComponentNameFactory;
import com.inspur.edp.web.formmetadata.i18n.component.strategy.namestrategy.ComponentNameType;
import com.inspur.edp.web.formmetadata.i18n.component.strategy.namestrategy.IComponentNameStrategy;
import com.inspur.edp.web.formmetadata.i18n.constant.I18nResourceConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
 * VUE输入类构件提取
 * @author wangzhiyuan
 */
public class TabPageResourceStrategy extends AbstractI18nResourceStrategy {
    @Override
    protected String getComponentName(HashMap<String, Object> component)  {
        IComponentNameStrategy componentNameStrategy = ComponentNameFactory.getInstance().creatComponentNameStrategy(ComponentNameType.TITLE);
        if (componentNameStrategy == null) {
            return null;
        }

        return componentNameStrategy.getComponentName(component);
    }
    @Override
    protected I18nResourceItemCollection extractAttributeI18nResourceItemCollection(String i18nResourceItemBaseId, HashMap<String, Object> currentComponent) {
        I18nResourceItemCollection i18nResourceItemCollection = new I18nResourceItemCollection();
        HashMap<String, Object> toolbar =(HashMap<String, Object>) currentComponent.get("toolbar");
        if(!toolbar.isEmpty()&&toolbar.containsKey("buttons")){
            List<Object> buttons=(List<Object>) toolbar.get("buttons");
            String currentComponentType = ComponentUtility.getInstance().getType(currentComponent);
            String currentComponentId = ComponentUtility.getInstance().getId(currentComponent);
            for (Object button : buttons) {
                HashMap<String, Object> data = (HashMap<String, Object>) button;
                String generatedComponentId = (String) data.get("id");
                I18nResourceItem footerButtonI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, generatedComponentId, (String)data.get("text"), (String)data.get("text"));
                this.addInCollection(i18nResourceItemCollection, footerButtonI18nResourceItem);

                if(data.containsKey("tipsText")){
                    String tipsText = (String) data.get("tipsText");
                    I18nResourceItem tipsTextI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId,  generatedComponentId+"/tipsText", tipsText, tipsText);
                    this.addInCollection(i18nResourceItemCollection, tipsTextI18nResourceItem);
                }

                if(data.containsKey("children")){
                    ArrayList<HashMap<String, String>> childrenButtons = (ArrayList<HashMap<String, String>>) data.get("children");
                    for (HashMap<String, String> childrenButton : childrenButtons) {
                        I18nResourceItem childButtonI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId,  childrenButton.get("id"), childrenButton.get("text"), childrenButton.get("text"));
                        this.addInCollection(i18nResourceItemCollection, childButtonI18nResourceItem);
                        if(childrenButton.containsKey("tipsText")){
                            String tipsText = (String) childrenButton.get("tipsText");
                            I18nResourceItem tipsTextI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId,  childrenButton.get("id")+"/tipsText", tipsText, tipsText);
                            this.addInCollection(i18nResourceItemCollection, tipsTextI18nResourceItem);
                        }
                    }
                }
            }
        }
        return i18nResourceItemCollection;
    }
}